﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using C.Entities;
using C.Unite;
using ChanExt;
using MySoft.Data;
using Newtonsoft.Json.Linq;

namespace ArtLand.OA.Project.ADM
{
    public partial class Adm_INRECDList : CommonPageBase
    {
        #region Inti
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

               // DataBaseHelper.BindDrp(Drp_MOTYPE, EnumData.DataBaseEunm.MoveType);

              //  btnNew.OnClientClick = Window2.GetShowReference("adm_inrecdchange.aspx", "添加物资出入库");

                //JObject defaultObj = new JObject();
                //defaultObj.Add("InTime", DateTime.Now.ToString("yyyy-MM-dd"));
                //defaultObj.Add("MATNUBID", "");
                //defaultObj.Add("MATNUBNO", "");
                //defaultObj.Add("MOTYPE", "");
                //defaultObj.Add("MOVQYT", 0);
                //defaultObj.Add("WHTFOR", "");
                //defaultObj.Add("ReMark", "");
                //btnNew.OnClientClick = Grid1.GetAddNewRecordReference(defaultObj, false);
                //txt_MATNUB.OnClientTriggerClick = Window1.GetSaveStateReference(txt_MATNUBID.ClientID, txt_MATNUB.ClientID)
                //    + Window1.GetShowReference("/ADM/Adm_BASICDView.aspx");
                BindGrid(GetWhere());
            }
        }
        #endregion

        #region BindGrid
        private void BindGrid(WhereClip wc)
        {
            Grid1.RecordCount = TbsDbSession.Default.From<Adm_INRECD>().Where(wc).Count();

            List<Adm_INRECD> items = TbsDbSession.Default.From<Adm_INRECD>().Where(wc).OrderBy(Adm_INRECD._.ID.Desc).Page(Grid1.PageSize, (Grid1.PageIndex + 1)).ToList();

            // 3.绑定到Grid
            Grid1.DataSource = items;
            Grid1.DataBind();
        }



        protected void Grid1_PageIndexChange(object sender, ChanExt.GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            BindGrid(GetWhere());
        }


        #endregion

        #region Events

        public WhereClip GetWhere()
        {
            WhereClip wc = WhereClip.All;
            //if (!string.IsNullOrEmpty(Request["did"]))
            //{
            //    wc = wc && Adm_BASICD._.BATYPE == int.Parse(Request["did"]);
            //}
            //if (!String.IsNullOrEmpty(ttbxMyBox2.Text))
            //{
            //    wc = wc && (Adm_BUDGET._.YERNUB.Contain(ttbxMyBox2.Text) || V_EmpInfo._.EMPLID.Contain(ttbxMyBox2.Text));
            //}
            return wc;
        }



        #region 搜索框
        //// 点击 TwinTriggerBox 的搜索按钮
        //protected void ttbxMyBox2_Trigger2Click(object sender, EventArgs e)
        //{
        //    if (!String.IsNullOrEmpty(ttbxMyBox2.Text))
        //    {
        //        // 执行搜索动作
        //        BindGrid(GetWhere());
        //        ttbxMyBox2.ShowTrigger1 = true;
        //    }
        //    else
        //    {
        //        Alert.ShowInTop("请输入你要搜索的关键词！");
        //    }
        //}

        //// 点击 TwinTriggerBox 的取消按钮
        //protected void ttbxMyBox2_Trigger1Click(object sender, EventArgs e)
        //{
        //    // 执行清空动作

        //    ttbxMyBox2.Text = "";
        //    ttbxMyBox2.ShowTrigger1 = false;
        //    BindGrid(GetWhere());
        //} 
        #endregion

        protected void Grid1_RowCommand(object sender, ChanExt.GridCommandEventArgs e)
        {
            int id = int.Parse(Grid1.DataKeys[e.RowIndex][0].ToString());
            if (e.CommandName == "Del")
            {
                TbsDbSession.Default.Delete<Adm_INRECD>(Adm_INRECD._.ID == id);
                Alert.ShowInTop("删除成功！");
                BindGrid(GetWhere());
            }
        }

        protected void Grid1_RowDataBound(object sender, ChanExt.GridRowEventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            int returnvalue = 0;
            int boolvalue = 0;
            Dictionary<int, Dictionary<string, string>> modifiedDict = Grid1.GetModifiedDict();
            foreach (int rowIndex in modifiedDict.Keys)
            {
                int rowID = Convert.ToInt32(Grid1.DataKeys[rowIndex][0]);
                //if (!modifiedDict[rowIndex].ContainsKey("AddTime"))
                //{
                //    boolvalue = 1;
                //}
                //else
                UpdateDataRow(modifiedDict[rowIndex], rowID);
            }
            returnvalue += modifiedDict.Keys.Count();
            // 新增数据
            List<Dictionary<string, string>> newAddedList = Grid1.GetNewAddedList();
            for (int i = newAddedList.Count - 1; i >= 0; i--)
            {
                if (!newAddedList[i].ContainsKey("AddTime"))
                {
                    boolvalue = 1;
                }
                else

                    UpdateDataRow(newAddedList[i], 0);
            }
            if (boolvalue == 0)
            {
                returnvalue += newAddedList.Count;
                if (returnvalue > 0)
                {
                    BindGrid(GetWhere());
                    Alert.Show("保存成功！");
                }
                else
                    Alert.Show("您未对任何行的数据进行修改！");
            }
            else
                Alert.Show("请输入入库时间！");
        }


        private static void UpdateDataRow(Dictionary<string, string> rowDict, int Dtype)
        {
            Adm_INRECD bud = new Adm_INRECD();
            if (Dtype == 0)
            {
                bud.DeAttach();
            }
            else
            {
                bud.ID = Dtype;
                bud.Attach();
            }
            //if (rowDict.ContainsKey("MATNUB"))
            //{
            //    bud = rowDict["MATNUB"];
            //}
            //if (rowDict.ContainsKey("MATNUBID"))
            //{
            //    bud.MATNUBID =int.Parse(rowDict["MATNUBID"]);
            //}
            if (rowDict.ContainsKey("AddTime"))
            {
                bud.AddTime = Convert.ToDateTime(rowDict["AddTime"]);
            }
            if (rowDict.ContainsKey("InOutType"))
            {
                bud.InOutType = rowDict["InOutType"];
            }
            if (rowDict.ContainsKey("MOTYPE"))
            {
                bud.MOTYPE = rowDict["MOTYPE"];
            }

            if (rowDict.ContainsKey("MOVQYT"))
            {
                bud.MOVQYT = int.Parse(rowDict["MOVQYT"]);
            }
            if (rowDict.ContainsKey("WHTFOR"))
            {
                bud.WHTFOR = rowDict["WHTFOR"];
            }
            if (rowDict.ContainsKey("REMARK"))
            {
                bud.REMARK = rowDict["REMARK"];
            }
            bud.AddUid = CookInfo.EAdminid();
            bud.AddName = CookInfo.EAdminTrueName();
            if (Dtype == 0)
            {
                TbsDbSession.Default.Insert<Adm_INRECD>(bud);
            }
            else
            {
                TbsDbSession.Default.Update<Adm_INRECD>(bud);
            }
        }
        #endregion

        protected void Button1_Click1(object sender, EventArgs e)
        {
            FormInfo.GridEdit(Grid1, Window2, "Adm_InrecdChange.aspx");
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            FormInfo.GridDelete<Adm_INRECD>(Grid1, Window2, new Adm_INRECD());
            BindGrid(GetWhere());
        }

        protected void Grid1_RowSelect(object sender, GridRowSelectEventArgs e)
        {
            //Button1.Enabled = true;
            //Button2.Enabled = true;

        }
    }
}